{% extends "base.html" %}
{% load djblets_extensions pipeline %}

{% block title %}JavaScript Unit Tests{% endblock %}

{% block css %}
{{block.super}}
{%  stylesheet "admin" %}
{%  stylesheet "js-tests" %}
{% endblock %}

{% block js_extensions %}{% endblock %}

{% block scripts-post %}
{{block.super}}
{%  javascript "djblets-config-forms" %}
{%  javascript "djblets-forms" %}
{%  javascript "djblets-integrations" %}
{%  javascript "admin" %}
{%  javascript "config-forms" %}
{%  javascript "ui" %}
{%  javascript "reviews" %}
{%  javascript "review-request-page" %}
{%  javascript "newReviewRequest" %}
{%  javascript "js-test-libs" %}
{%  javascript "widgets" %}

{%  block scripts-test %}{% endblock %}

<script>
    RB.ajaxOptions.enableQueuing = false;
    RB.ajaxOptions.enableIndicator = false;

    // Disable animations so we don't have to worry about them in tests.
    $.fx.off = true;

    // Disable throttling of callbacks when possible.
    _.throttle = function(cb) {
        return cb;
    };

    window.$testsScratch = $("<div id='tests-scratch'/>")
        .width(1)
        .height(1)
        .move(-10000, -10000, 'absolute')
        .appendTo(document.body);

    /*
     * By default, Jasmine 3.0+ runs tests in random order, to help catch
     * interdependencies between tests. While good in theory, a lot can
     * break in practice, in unpredictable ways. We disable by default,
     * allowing users to opt in instead.
     */
    jasmine.getEnv().configure({
        random: false
    });

    /*
     * Ensure we're working with a consistent "local" timezone for moment.js,
     * so two people don't see different results.
     */
    moment.tz.setDefault('US/Pacific');

    beforeEach(function() {
        /*
         * Set the session to be authenticated by default. If tests need
         * it to be unauthenticated, they can reset this.
         */
        RB.UserSession.instance = null;
        RB.UserSession.create({
            authenticated: true,
            commentsOpenAnIssue: true,
            username: 'testuser',
            userPageURL: '{{SITE_ROOT}}users/test/'
        });

        /*
         * Capture all assertions, so we can sanely inspect them.
         * We don't want to call the original version, though, or we'll
         * possibly get errors in the console.
         *
         * We do this after creating the UserSession, since that will also
         * assert.
         */
        spyOn(console, 'assert').and.callFake(function(condition, msg) {
            if (!condition) {
                throw Error(msg);
            }
        });

        /*
         * We don't have actual gravatar sources when running tests, so avoid
         * spurious error output and HTTP requests.
         */
        spyOn($.fn, 'retinaGravatar')
            .and.callFake(function() { return this; });
    });

    afterEach(function() {
        $testsScratch.empty();
    });
</script>
{% endblock %}
